<?php
/**
 * File containing the RandomCreditCardNumberProcessor
 *
 * PHP Version 5
 *
 * @category Processors
 * @package  Processors
 * @author   Martin Schlecht <herrlollinger@gmail.com>
 * @license  MIT License
 * @link     http://code.google.com/p/universal-test-data
 */

/**
 * Processor class "RandomCreditCardNumberProcessor"
 *
 * Processor which is used to process tags which implements the random generation of credit card numbers within the text.
 *
 * @category   Processors
 * @package    Processors
 * @subpackage DateProcessors
 * @author     Martin Schlecht <herrlollinger@gmail.com>
 * @copyright  see @author
 * @license    MIT License
 * @link       http://code.google.com/p/universal-test-data
 *
 * The followings are the available attributes of the processor class
 * @property string $text, the text which should be processed
 */
class RandomCreditCardNumberProcessor
{
	private $text;
	
	/**
	 * The processor adds random generated credit card numbers and repeats the dates n times
	 *
	 * @param String $text, the text to process
	 *
	 * @return String
	 */
	public function process($text) {
		$this->text = $text;
		
		preg_match_all("#\s*{(.*?)}#si", $this->text, $matches);
		
		foreach ($matches[1] AS $m) {
			$string = "{" . $m . "}";
			$m = explode(":", $m);
			
			/**
			 * {randomCreditCardNumber:} 
			 * Random credit card number for given institution
			 * 
			 *	Issuer	                    Identifier	                    Card Number Length
			 *	Diner's Club            	300xxx-305xxx, 36xxxx, 38xxxx	14
			 *	American Express	        34xxxx, 37xxxx	                15
			 *	VISA	                    4xxxxx	                        13, 16
			 *	MasterCard	                51xxxx-55xxxx	                16
			 *	Discover	                6011xx	                        16
			 */
			if ($m[0] == "randomCreditCardNumber") {
				$parameter = explode(",", $m[1]);
				
				if (!isset($parameter[1]) || !is_numeric($parameter[1])) {
					$parameter[1] = 0;
				}
				
				$time += $parameter[1] * 60*60*24;
				
				if (!isset($parameter[2]) || !is_numeric($parameter[2])) {
					$parameter[2] = 1;
				}
				
				$replacement = date($parameter[0], $time);
			
				$this->text = str_replace($string, $this->manageRepeation($replacement, $string, $parameter[2]), $this->text);
			}
		}
		
		return $this->text;
	}
	
	/**
	 * Manages the repeation of values
	 * 
	 * @param String $value,  the value which should be replaced
	 * @param String $string, the string for the cockie
	 * @param String $times,  the number of times the value should be repeated
	 * 
	 * @return String
	 */
	public function manageRepeation($value, $string, $times) {
		$replacement = "";
		
		if (Yii::app()->session[$string . "_times"] >= $times || !isset(Yii::app()->session[$string . "_times"])) {
			Yii::app()->session[$string . "_replacement"] = $value;
			Yii::app()->session[$string . "_times"] = 1;
							
			$replacement = $value;
		} else {
			$number = Yii::app()->session[$string . "_replacement"];
			$times = Yii::app()->session[$string . "_times"] + 1;
			
			Yii::app()->session[$string . "_times"] = $times;
			
			$replacement = $number;
		}
		
		Yii::app()->session['resetManager']->add($string . "_replacement");
		Yii::app()->session['resetManager']->add($string . "_times");
		
		return $replacement;
	}
	
	/**
	 * Returns the documentation
	 *
	 * @return String
	 */
	 public function documentation() {
		return array(
			array('{actualDate:format,dayOffset}', 'Actual date with given layout'),
			array('{actualDateTime:format,dayOffset,hourOffset}', 'Actual date and time with given layout'),
			array('{actualUnixTimestamp:}', 'Actual unix timestamp'),
		);
	 }
	 
	/**
	 * Prints the documentation
	 *
	 * @return string
	 */
	public function print_documentation() {
		foreach ($this->documentation() AS $docu) {
			echo '<span style="font-family:monospace;font-size:11px">' . $docu[0] . '</span><br/>' . $docu[1] . '<br/>';
		}
	}
}
?>